package com.yuanchu.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.store.dao.YoshopRegionDao;
import com.yuanchu.store.dao.YoshopUserAddressDao;
import com.yuanchu.store.dao.YoshopUserDao;
import com.yuanchu.store.dto.AddressDto;
import com.yuanchu.store.dto.EditAddressDto;
import com.yuanchu.store.entity.YoshopUserAddressEntity;
import com.yuanchu.store.entity.YoshopUserEntity;
import com.yuanchu.store.service.YoshopUserAddressService;
import com.yuanchu.store.service.YoshopUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service("yoshopUserAddressService")
public class YoshopUserAddressServiceImpl extends ServiceImpl<YoshopUserAddressDao, YoshopUserAddressEntity> implements YoshopUserAddressService {

    @Resource
    private YoshopRegionDao regionDao;
    @Resource
    private YoshopUserDao userDao;
    @Resource
    private YoshopUserService userService;

    @Override
    public List<YoshopUserAddressEntity> queryUserAddress(Integer userId, Integer storeId) {
        List<YoshopUserAddressEntity> list = this.list(new QueryWrapper<YoshopUserAddressEntity>()
                        .eq("store_id", storeId)
                .eq("user_id", userId).eq("is_delete", 0));
        list.forEach(address -> {
            Integer regionId = address.getRegionId();
            Map<String, Object> region = regionDao.getAddressByRegionId(regionId);
            address.setRegion(region);
        });
        return list;
    }

    @Override
    public Integer queryUserDefaultAddressId(Integer userId) {
        YoshopUserEntity user = userDao.selectById(userId);
        return user.getAddressId();
    }

    @Override
    public YoshopUserAddressEntity queryUserDetailAddress(Integer userId, Integer addressId) {
        YoshopUserAddressEntity address = this.getById(addressId);
        address.setRegion(regionDao.getAddressByRegionId(address.getRegionId()));
        return address;
    }

    @Override
    @Transactional
    public boolean editUserAddress(Integer userId, EditAddressDto addressForm) {
        YoshopUserAddressEntity address = new YoshopUserAddressEntity();
        BeanUtils.copyProperties(addressForm.getForm(), address);
        address.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
        UpdateWrapper<YoshopUserAddressEntity> updateWrapper = new UpdateWrapper<YoshopUserAddressEntity>()
                .eq("user_id", userId)
                .eq("address_id", addressForm.getAddressId());
        List<AddressDto.Region> region = addressForm.getForm().getRegion();
        // 县
        address.setRegionId(region.get(2).getValue());
        // 市
        address.setCityId(region.get(1).getValue());
        // 省
        address.setProvinceId(region.get(0).getValue());

        return this.update(address, updateWrapper);
    }

    @Override
    @Transactional
    public void addUserAddress(Integer userId, EditAddressDto form) {
        YoshopUserAddressEntity address = new YoshopUserAddressEntity();
        BeanUtils.copyProperties(form.getForm(), address);
        List<AddressDto.Region> region = form.getForm().getRegion();
        // 县
        address.setRegionId(region.get(2).getValue());
        // 市
        address.setCityId(region.get(1).getValue());
        // 省
        address.setProvinceId(region.get(0).getValue());
        address.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
        address.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
        address.setStoreId(10001);
        address.setUserId(userId);
        this.save(address);
    }

    @Override
    @Transactional
    public boolean setUserDefaultAddress(Integer userId, Integer addressId, Integer storeId) {

        return userService.update(new UpdateWrapper<YoshopUserEntity>()
                .eq("store_id", storeId)
                .eq("user_id", userId)
                .set("address_id", addressId));

    }

    @Override
    @Transactional
    public boolean removeUserAddress(Integer userId, EditAddressDto addressForm, Integer storeId) {
        String addressId = addressForm.getAddressId();

        return this.update(new UpdateWrapper<YoshopUserAddressEntity>()
                .eq("store_id", storeId)
                .eq("user_id", userId)
                .eq("address_id", addressId).set("is_delete", 1)
        );
    }


}